﻿/*
Copyright © 2011 Nokia Corporation. All rights reserved.
Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
Java and all Java-based marks are trademarks or registered trademarks of 
Sun Microsystems, Inc. Other product and company names mentioned herein may be 
trademarks or trade names of their respective owners.

Subject to the conditions below, you may, without charge:

·  Use, copy, modify and/or merge copies of this software and 
   associated content and documentation files (the “Software”)

·  Publish, distribute, sub-licence and/or sell new software 
   derived from or incorporating the Software.

Some of the documentation, content and/or software maybe licensed under open source software or other licenses.  To the extent such documentation, content and/or software are included, licenses and/or other terms and conditions shall apply in addition and/or instead of this notice. The exact terms of the licenses, disclaimers, acknowledgements and notices are reproduced in the materials provided.

This file, unmodified, shall be included with all copies or substantial portions
of the Software that are distributed in source code form.

The Software cannot constitute the primary value of any new software derived 
from or incorporating the Software.

Any person dealing with the Software shall not misrepresent the source of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using System.Collections.ObjectModel;

namespace Carpool
{

    [DataContract]
    public class DCDataClasses
    {
        [DataMember]
        public bool isError { get; set; }
        [DataMember]
        public string errorString { get; set; }
        [DataMember]
        public string statusString { get; set; }

        [DataMember]
        public string username { get; set; }
        [DataMember]
        public string password { get; set; }
        [DataMember]
        public string karpoid { get; set; }
        [DataMember]
        public Boolean newmessagesavailable { get; set; }


        public DCDataClasses() 
        {
            username = MainPage.username;
            karpoid = MainPage.userkarpoid;
            password = MainPage.userpassword;
            newmessagesavailable = false;
            isError = false;
            errorString = "";
            statusString = "ok";
        }
    }

    [DataContract]
    public class DCCarpoolMember 
    {
        [DataMember]
        public string carpoolmemberkarpoid { get; set; }
        [DataMember]
        public string carpoolkarpoid { get; set; }
        [DataMember]
        public string ownerkarpoid { get; set; }
        [DataMember]
        public string memberkarpoid { get; set; }
        [DataMember]
        public string firstname { get; set; }
        [DataMember]
        public string lastname { get; set; }
        [DataMember]
        public string phone { get; set; }
        [DataMember]
        public string hometown { get; set; }
        [DataMember]
        public string status { get; set; }
        [DataMember]
        public string picklat { get; set; }
        [DataMember]
        public string picklon { get; set; }

        public DCCarpoolMember() { }

        public DCCarpoolMember(string _carpoolmemberkarpoid, string _carpoolkarpoid, string _ownerkarpoid,
            string _memberkarpoid, string _firstname, string _lastname, string _phone, string _hometown,
            string _status, string _picklat, string _picklon)
        {
            carpoolmemberkarpoid = _carpoolmemberkarpoid;
            carpoolkarpoid = _carpoolkarpoid;
            ownerkarpoid = _ownerkarpoid;
            memberkarpoid = _memberkarpoid;
            firstname = _firstname;
            lastname = _lastname;
            phone = _phone;
            hometown = _hometown;
            status = _status;
            picklat = _picklat;
            picklon = _picklon;
        }

    }


    [DataContract]
    public class DCCarPool 
    {
        [DataMember]
        public string carpoolkarpoid { get; set; }
        [DataMember]
        public string ownerkarpoid { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string description { get; set; }
        [DataMember]
        public string routekarpoid { get; set; }
        [DataMember]
        public string routename { get; set; }
        [DataMember]
        public DCCarpoolMember[] members { get; set; }

        public DCCarPool() { }

        public DCCarPool(string _ownerkarpoid, string _carpoolkarpoid, string _name, string _description, string _routekarpoid, string _routename, DCCarpoolMember[] _members) 
        {

            ownerkarpoid = _ownerkarpoid;
            carpoolkarpoid = _carpoolkarpoid;
            name = _name;
            description = _description;
            routekarpoid = _routekarpoid;
            routename = _routename;
            members = _members;
        }

    }

    [DataContract]
    public class DCCarPoolList : DCDataClasses
    {
        [DataMember]
        public DCCarPool[] carpoollist { get; set; }

        public DCCarPoolList() { }

        public DCCarPoolList(String _karpoid, DCCarPool[] _carpoollist) 
        {
            karpoid = _karpoid;
            carpoollist = _carpoollist;
        }
    }

    public class DCCarPoolListJsonUtil
    {
        public DCCarPoolListJsonUtil() { }

        public static string WriteFromObject(DCCarPoolList dccarpoollist)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCCarPoolList), new Type[]{typeof(DCCarPool), typeof(DCCarpoolMember)});
            ser.WriteObject(ms, dccarpoollist);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCCarPoolList ReadToObject(string json)
        {
            DCCarPoolList deserializedDCCarPoolList = new DCCarPoolList();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedDCCarPoolList.GetType(), new Type[] { typeof(DCCarPool), typeof(DCCarpoolMember) });
            deserializedDCCarPoolList = ser.ReadObject(ms) as DCCarPoolList;
            ms.Close();
            return deserializedDCCarPoolList;
        }
    }

 
    [DataContract]
    public class DCCarPoolMembersList : DCDataClasses
    {
        [DataMember]
        public DCCarpoolMember[] members { get; set; }

        public DCCarPoolMembersList() { }

        public DCCarPoolMembersList(String _karpoid, DCCarpoolMember[] _members)
        {
            karpoid = _karpoid;
            members = _members;
        }
    }

    public class DCCarPoolMembersListJsonUtil
    {
        public DCCarPoolMembersListJsonUtil() { }

        public static string WriteFromObject(DCCarPoolMembersList dccarpoolmemberslist)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCCarPoolMembersList), new Type[] { typeof(DCCarpoolMember) });
            ser.WriteObject(ms, dccarpoolmemberslist);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCCarPoolMembersList ReadToObject(string json)
        {
            DCCarPoolMembersList deserializedDCCarPoolMembersList = new DCCarPoolMembersList();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedDCCarPoolMembersList.GetType(), new Type[] { typeof(DCCarpoolMember) });
            deserializedDCCarPoolMembersList = ser.ReadObject(ms) as DCCarPoolMembersList;
            ms.Close();
            return deserializedDCCarPoolMembersList;
        }
    }



    [DataContract]
    public class DCPersonalProfile : DCDataClasses
    {
        [DataMember]
        public string confirmpassword { get; set; }
        [DataMember]
        public bool deleteaccount { get; set; }
        [DataMember]

        public string firstname { get; set; }
        [DataMember]
        public string lastname { get; set; }
        [DataMember]
        public string hometown { get; set; }
        [DataMember]
        public string phone { get; set; }
        [DataMember]
        public string alias { get; set; }

        [DataMember]
        public bool showname { get; set; }
        [DataMember]
        public bool showphone { get; set; }
        [DataMember]
        public bool showhometown { get; set; }

        public DCPersonalProfile() { }

        public DCPersonalProfile(string _username, string _password, string _confirmpassword, 
            bool _deleteaccount, string _karpoid,
            string _firstname, string _lastname, string _hometown, string _phone, string _alias,
            bool _showname, bool _showphone, bool _showhometown)
        {
            username = _username;
            password = _password;
            confirmpassword = _confirmpassword;
            deleteaccount = _deleteaccount;
            
            karpoid = _karpoid;
            firstname = _firstname;
            lastname = _lastname;
            hometown = _hometown;
            phone = _phone;
            alias = _alias;

            showname = _showname;
            showphone = _showphone;
            showhometown = _showhometown;

            isError = false;
            errorString = "";
            statusString = "";
        }
    }

    public class DCPersonalProfileJsonUtil
    {
        public DCPersonalProfileJsonUtil() { }

        public static string WriteFromObject(DCPersonalProfile dcpersonalprofile)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCPersonalProfile));
            ser.WriteObject(ms, dcpersonalprofile);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCPersonalProfile ReadToObject(string json)
        {
            DCPersonalProfile deserializedDCPersonalProfile = new DCPersonalProfile();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedDCPersonalProfile.GetType());
            deserializedDCPersonalProfile = ser.ReadObject(ms) as DCPersonalProfile;
            ms.Close();
            return deserializedDCPersonalProfile;
        }
    }


    [DataContract]
    public class DCUserList : DCDataClasses
    {

        [DataMember]
        public DCPersonalProfile[] userlist { get; set; }
        [DataMember]
        public bool phoneRequired { get; set; }
        [DataMember]
        public bool firstnameRequired { get; set; }
        [DataMember]
        public bool lastnameRequired { get; set; }
        [DataMember]
        public bool hometownRequired { get; set; }

        public DCUserList() { }

        public DCUserList(String _karpoid, bool _phoneRequired, bool _firstnameRequired, bool _lastnameRequired, bool _hometownRequired, DCPersonalProfile[] _userlist) 
        {
            karpoid = _karpoid;
            phoneRequired = _phoneRequired;
            firstnameRequired = _firstnameRequired;
            lastnameRequired = _lastnameRequired;
            hometownRequired = _hometownRequired;
            userlist = _userlist;
        }
    }

    public class DCUserListJsonUtil
    {
        public DCUserListJsonUtil() { }

        public static string WriteFromObject(DCUserList dcuserlist)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCUserList), new Type[] { typeof(DCPersonalProfile) });
            ser.WriteObject(ms, dcuserlist);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCUserList ReadToObject(string json)
        {
            DCUserList deserializedDCUserList = new DCUserList();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedDCUserList.GetType(), new Type[] { typeof(DCPersonalProfile) });
            deserializedDCUserList = ser.ReadObject(ms) as DCUserList;
            ms.Close();
            return deserializedDCUserList;
        }
    }


    [DataContract]
    public class DCMyTrip 
    {
        // KarpoCarpoolMembersIndex
        [DataMember]
        public string memberindexkarpoid { get; set; }  // KarpoCarpoolMembersIndex.karpoid
        [DataMember]
        public string carpoolkarpoid { get; set; }      // KarpoCarpoolMembersIndex.carpoolkarpoid
        [DataMember]
        public string picklat { get; set; }             // KarpoCarpoolMembersIndex.picklat
        [DataMember]
        public string picklon { get; set; }             // KarpoCarpoolMembersIndex.picklon

        [DataMember]
        public string ownername { get; set; }
        [DataMember]
        public string ownerphone { get; set; }
        [DataMember]
        public string ownerhometown { get; set; }
        [DataMember]
        public string owneralias { get; set; }
        
        //KarpoRouteIndex
        [DataMember]
        public string routeindexkarpoid { get; set; }   // KarpoRouteIndex.karpoid
        [DataMember]
        public string ownerkarpoid { get; set; }        // KarpoRouteIndex.ownerkarpoid
        [DataMember]
        public string routename { get; set; }           // KarpoRouteIndex.name
        [DataMember]
        public string routedescription { get; set; }    // KarpoRouteIndex.description
        [DataMember]
        public string routetime { get; set; }           // KarpoRouteIndex.time
        [DataMember]
        public string routerepeat { get; set; }         // KarpoRouteIndex.repeat

        [DataMember]
        public string routefromstreet { get; set; }     // KarpoRouteIndex.route[0].street
        [DataMember]
        public string routefromcity { get; set; }       // KarpoRouteIndex.route[0].city
        [DataMember]
        public string routefrompostcode { get; set; }   // KarpoRouteIndex.route[0].postcode
        [DataMember]
        public string routefromcountry { get; set; }    // KarpoRouteIndex.route[0].country
        [DataMember]
        public string routefromlat { get; set; }        // KarpoRouteIndex.route[0].latitude
        [DataMember]
        public string routefromlon { get; set; }        // KarpoRouteIndex.route[0].longitude

        [DataMember]
        public string routetostreet { get; set; }       // KarpoRouteIndex.route[1].street
        [DataMember]
        public string routetocity { get; set; }         // KarpoRouteIndex.route[1].city
        [DataMember]
        public string routetopostcode { get; set; }     // KarpoRouteIndex.route[1].postcode
        [DataMember]
        public string routetocountry { get; set; }      // KarpoRouteIndex.route[1].country
        [DataMember]
        public string routetolat { get; set; }          // KarpoRouteIndex.route[1].latitude
        [DataMember]
        public string routetolon { get; set; }          // KarpoRouteIndex.route[1].longitude

        // KarpoCarpoolActivationIndex
        [DataMember]
        public string active { get; set; }              // KarpoCarpoolActivationIndex.active [1 | 0]


        public DCMyTrip() { }

    }

    [DataContract]
    public class DCMyTripList : DCDataClasses
    {
        [DataMember]
        public DCMyTrip[] mytriplist { get; set; }

        public DCMyTripList() { }

        public DCMyTripList(String _karpoid) 
        {
            karpoid = _karpoid;
        }
    }

    public class DCMyTripListJsonUtil
    {
        public DCMyTripListJsonUtil() { }

        public static string WriteFromObject(DCMyTripList dcmytriplist)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCMyTripList), new Type[] { typeof(DCMyTrip) });
            ser.WriteObject(ms, dcmytriplist);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCMyTripList ReadToObject(string json)
        {
            DCMyTripList deserializedDCMyTripList = new DCMyTripList();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedDCMyTripList.GetType(), new Type[] { typeof(DCMyTrip) });
            deserializedDCMyTripList = ser.ReadObject(ms) as DCMyTripList;
            ms.Close();
            return deserializedDCMyTripList;
        }
    }


    [DataContract]
    public class DCCarPoolActivation 
    {
        [DataMember]
        public string carpoolkarpoid { get; set; }
        [DataMember]
        public string active { get; set; }
        [DataMember]
        public string alias { get; set; }
        [DataMember]
        public string lat { get; set; }
        [DataMember]
        public string lon { get; set; }

        public DCCarPoolActivation() { }

        public DCCarPoolActivation(string _carpoolkarpoid)
        {
            carpoolkarpoid = _carpoolkarpoid;
        }
    }

    [DataContract]
    public class DCCarPoolActivationList : DCDataClasses
    {
        [DataMember]
        public DCCarPoolActivation[] carpoolactivationlist { get; set; }

        public DCCarPoolActivationList() { }

    }

    public class DCCarPoolActivationListJsonUtil
    {
        public DCCarPoolActivationListJsonUtil() { }

        public static string WriteFromObject(DCCarPoolActivationList dccarpoolactivation)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCCarPoolActivationList), new Type[] { typeof(DCCarPoolActivation) });
            ser.WriteObject(ms, dccarpoolactivation);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCCarPoolActivationList ReadToObject(string json)
        {
            DCCarPoolActivationList deserializedDCCarPoolActivation = new DCCarPoolActivationList();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedDCCarPoolActivation.GetType(), new Type[] { typeof(DCCarPoolActivation) });
            deserializedDCCarPoolActivation = ser.ReadObject(ms) as DCCarPoolActivationList;
            ms.Close();
            return deserializedDCCarPoolActivation;
        }
    }


    [DataContract]
    public class DCRoutePoint
    {
        [DataMember]
        public string type { get; set; }
        [DataMember]
        public bool pointset { get; set; }
        [DataMember]
        public double latitude { get; set; }
        [DataMember]
        public double longitude { get; set; }
        [DataMember]
        public string country { get; set; }
        [DataMember]
        public string countrycode { get; set; }
        [DataMember]
        public string state { get; set; }
        [DataMember]
        public string city { get; set; }
        [DataMember]
        public string district { get; set; }
        [DataMember]
        public string street { get; set; }
        [DataMember]
        public string postcode { get; set; }
        [DataMember]
        public double distance { get; set; }
        [DataMember]
        public long traveltime { get; set; }
        [DataMember]
        public string routepolyline { get; set; }

        public DCRoutePoint()
        {
            type = "";
            pointset = false;
            latitude = 0.0;
            longitude = 0.0;
            country = "";
            countrycode = "";
            state = "";
            city = "";
            district = "";
            street = "";
            postcode = "";
            distance = 0.0;
            traveltime = 0;
            routepolyline = "[]";
        }

    }

    [DataContract]
    public class DCRoute : DCDataClasses
    {
        [DataMember]
        public string routekarpoid { get; set; }
        [DataMember]
        public string ownerkarpoid { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string description { get; set; }
        [DataMember]
        public string time { get; set; }
        [DataMember]
        public string repeat { get; set; }
        [DataMember]
        public DCRoutePoint[] route { get; set; }
        [DataMember]
        public string lat1 { get; set; }
        [DataMember]
        public string lon1 { get; set; }
        [DataMember]
        public string lat2 { get; set; }
        [DataMember]
        public string lon2 { get; set; }
        [DataMember]
        public string showownername { get; set; }
        [DataMember]
        public string showownerhometown { get; set; }
        [DataMember]
        public string showownerphone { get; set; }
        [DataMember]
        public string carpoolkarpoid { get; set; }


        [DataMember]
        public DCPersonalProfile ownerinfo { get; set; }

        public DCRoute() 
        {
            routekarpoid = "0";
            ownerkarpoid = "";
            name = "";
            description = "";
            time = "";
            repeat = "";
            lat1 = "";
            lon1 = "";
            lat2 = "";
            lon2 = "";
            showownername = "0";
            showownerphone = "0";
            showownerhometown = "0";
            carpoolkarpoid = "";
        }

    }

    public class DCRouteJsonUtil
    {
        public DCRouteJsonUtil() { }

        public static string WriteFromObject(DCRoute dcroute)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCRoute), new Type[] { typeof(DCRoutePoint), typeof(DCPersonalProfile) });
            ser.WriteObject(ms, dcroute);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCRoute ReadToObject(string json)
        {
            DCRoute deserializedDCRoute = new DCRoute();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedDCRoute.GetType(), new Type[] { typeof(DCRoutePoint), typeof(DCPersonalProfile) });
            deserializedDCRoute = ser.ReadObject(ms) as DCRoute;
            ms.Close();
            return deserializedDCRoute;
        }
    }


    [DataContract]
    public class DCRouteList : DCDataClasses
    {
        [DataMember]
        public DCRoute[] routelist { get; set; }

        // datamembers fromorto,lat1,lon1,lat2,lon2 are needed in the GetRoutesInRect case
        [DataMember]
        public string fromorto { get; set; }
        [DataMember]
        public double lat1 { get; set; }
        [DataMember]
        public double lon1 { get; set; }
        [DataMember]
        public double lat2 { get; set; }
        [DataMember]
        public double lon2 { get; set; }

        public DCRouteList() { }

        public DCRouteList(String _karpoid)
        {
            karpoid = _karpoid;
        }
    }

    public class DCRouteListJsonUtil
    {
        public DCRouteListJsonUtil() { }

        public static string WriteFromObject(DCRouteList dcroutelist)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCRouteList), new Type[] { typeof(DCRoute) });
            ser.WriteObject(ms, dcroutelist);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCRouteList ReadToObject(string json)
        {
            DCRouteList deserialized = new DCRouteList();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserialized.GetType(), new Type[] { typeof(DCRoute) });
            deserialized = ser.ReadObject(ms) as DCRouteList;
            ms.Close();
            return deserialized;
        }
    }

    [DataContract]
    public class DCJoinRequest : DCDataClasses
    {
        [DataMember]
        public string joinrequestkarpoid { get; set; }
        [DataMember]
        public string carpoolkarpoid { get; set; }
        [DataMember]
        public string routekarpoid { get; set; }
        [DataMember]
        public string ownerkarpoid { get; set; }
        [DataMember]
        public string travellerkarpoid { get; set; }
        [DataMember]
        public string showname { get; set; }
        [DataMember]
        public string showhometown { get; set; }
        [DataMember]
        public string showphone { get; set; }
        [DataMember]
        public string message { get; set; }
        [DataMember]
        public double picklat { get; set; }
        [DataMember]
        public double picklon { get; set; }

        public DCJoinRequest() { }

    }


    public class DCJoinRequestJsonUtil
    {
        public DCJoinRequestJsonUtil() { }

        public static string WriteFromObject(DCJoinRequest obj)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCJoinRequest));
            ser.WriteObject(ms, obj);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCJoinRequest ReadToObject(string json)
        {
            DCJoinRequest deserialized = new DCJoinRequest();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserialized.GetType());
            deserialized = ser.ReadObject(ms) as DCJoinRequest;
            ms.Close();
            return deserialized;
        }
    }


    [DataContract]
    public class DCMessage : DCDataClasses
    {
        [DataMember]
        public string messagekarpoid { get; set; }
        [DataMember]
        public string senderkarpoid { get; set; }
        [DataMember]
        public string receiverkarpoid { get; set; }
        [DataMember]
        public string message { get; set; }
        [DataMember]
        public string statuschangetime { get; set; }
        [DataMember]
        public string status { get; set; }
        [DataMember]
        public string senderusername { get; set; }
        [DataMember]
        public string receiverusername { get; set; }

        public DCMessage() { }

    }


    [DataContract]
    public class DCMessageList : DCDataClasses
    {
        [DataMember]
        public string condition { get; set; } // "NEW", "ALL", "SENT"

        [DataMember]
        public DCMessage[] messagelist { get; set; }

        public DCMessageList() { }

        public DCMessageList(string _karpoid, string _condition) 
        {
            karpoid = _karpoid;
            condition = _condition; 
        }



    }

    public class DCMessageListJsonUtil
    {
        public DCMessageListJsonUtil() { }

        public static string WriteFromObject(DCMessageList dcmessagelist)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCMessageList), new Type[] { typeof(DCMessage) });
            ser.WriteObject(ms, dcmessagelist);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCMessageList ReadToObject(string json)
        {
            DCMessageList deserialized = new DCMessageList();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserialized.GetType(), new Type[] { typeof(DCMessage) });
            deserialized = ser.ReadObject(ms) as DCMessageList;
            ms.Close();
            return deserialized;
        }
    }


    [DataContract]
    public class DCKarpoIdList : DCDataClasses
    {
        [DataMember]
        public string[] karpoids { get; set; } // just a list of karpoids

        public DCKarpoIdList() { }

        public DCKarpoIdList(string _karpoid, string _password, string _username)
        {
            karpoid = _karpoid;
            password = _password;
            username = _username;
        }

    }

    public class DCKarpoIdListJsonUtil
    {
        public DCKarpoIdListJsonUtil() { }

        public static string WriteFromObject(DCKarpoIdList dckarpoidlist)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCKarpoIdList));
            ser.WriteObject(ms, dckarpoidlist);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCKarpoIdList ReadToObject(string json)
        {
            DCKarpoIdList deserialized = new DCKarpoIdList();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserialized.GetType());
            deserialized = ser.ReadObject(ms) as DCKarpoIdList;
            ms.Close();
            return deserialized;
        }
    }

    // --

    [DataContract]
    public class DCMessageOut : DCDataClasses
    {
        [DataMember]
        public string[] receiverskarpoidarray { get; set; } // just a list of karpoids

        [DataMember]
        public string message = "";

        [DataMember]
        public string statuschangetime { get; set; }

        public DCMessageOut() { }

        public DCMessageOut(string _karpoid, string _password, string _username)
        {
            karpoid = _karpoid;
            password = _password;
            username = _username;
        }

    }

    public class DCMessageOutJsonUtil
    {
        public DCMessageOutJsonUtil() { }

        public static string WriteFromObject(DCMessageOut dckarpoidlist)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DCMessageOut));
            ser.WriteObject(ms, dckarpoidlist);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static DCMessageOut ReadToObject(string json)
        {
            DCMessageOut deserialized = new DCMessageOut();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserialized.GetType());
            deserialized = ser.ReadObject(ms) as DCMessageOut;
            ms.Close();
            return deserialized;
        }
    }


    public class KarpoMessage
    {
        public String From { get; set; }
        public String To { get; set; }
        public String Time { get; set; }
        public String Message { get; set; }

        public KarpoMessage(String _From, String _To, String _Time, String _Message)
        {
            this.From = _From;
            this.To = _To;
            this.Time = _Time;
            this.Message = _Message;
        }

    }

    public class KarpoMessages : ObservableCollection<KarpoMessage>
    {
        public KarpoMessages()
        {
            /*
            Add(new KarpoMessage("Lähettäjä1", "Vastaanottaja",
                    "Aika", "Tämä on varsinainen viesti"));
            Add(new KarpoMessage("Lähettäjä2", "Vastaanottaja",
                     "Aika", "Tämä on varsinainen viesti"));
            Add(new KarpoMessage("Lähettäjä3", "Vastaanottaja",
             "Aika", "Tämä on varsinainen viesti"));
            Add(new KarpoMessage("Lähettäjä4", "Vastaanottaja",
             "Aika", "Tämä on varsinainen viesti"));
             */
        }

    }

    // -------- RESTORE -----------

    [DataContract]
    public class RestoreUI
    {
        [DataMember]
        public string appcase { get; set; }

        [DataMember]
        public int currentcarpoolind { get; set; }

        public RestoreUI() { }
        public RestoreUI(string _appcase) { appcase = _appcase; }

    }

    public class RestoreUIJsonUtil
    {
        public RestoreUIJsonUtil() { }

        public static string WriteFromObject(RestoreUI restoreui)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RestoreUI));
            ser.WriteObject(ms, restoreui);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);

        }

        public static RestoreUI ReadToObject(string json)
        {
            RestoreUI deserialized = new RestoreUI();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserialized.GetType());
            deserialized = ser.ReadObject(ms) as RestoreUI;
            ms.Close();
            return deserialized;
        }
    }


}



